﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_HealthMedicineUpdForm
 * 类型：       控制层
 * 功能说明：   健康管家-用药信息维护页面
 * 作者：       黄鑫
 * 创建日期：   2007/09/25
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.System.Validator;
using Shpm.System.Web.Control;
using log4net;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;

public partial class App_Input_HealthMedicineUpdForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_HealthMedicineUpdForm));
    public const int DEFAULTROW_WORKEXP = 1;
    public const int DETAIL_COL_COUNT = 5;
    public const int COL_MEDICINE_NAME = 0;
    public const int COL_MEDICINE_APPROACH = 1;
    public const int COL_MEDICINE_FREQUENCY = 2;
    public const int COL_MEDICINE_QUANTITY = 3;
    public const int COL_MEDICINE_UNIT = 4;
    public const int COL_MEDICINE_TYPE = 4;
    private int intAddRows = DEFAULTROW_WORKEXP;
    IList lstMedicineType = null;
    IList lstMedicineApproach = null;
    IList lstMedicineFrequency = null;
    IList lstMedicineUnit = null;

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // 取得当前登录的医生id信息
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
            string emplyRole = mstEmpBean == null ? "" : mstEmpBean.Role;
            string emplyJob = mstEmpBean == null ? "" : mstEmpBean.EmployeeJob;
            string emplyLevel = mstEmpBean == null ? "" : mstEmpBean.EmployeeLevel;

            ViewState["DOCTOR_ID"] = emplyId;
            ViewState["EMPLY_LEVEL"] = emplyLevel;
            ViewState["MEMBER_MEDICINE_ID"] = "0";
            ViewState["HISTORY_FLG"] = "0";

            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                ViewState["ParamPageIndex"] = Context.Items["ParamPageIndex"];
            }

            ViewState["MEMBER_NO"] = Convert.ToString(Context.Items["MEMBER_NO"]);
            this.ViewState["NEW_FLG"] = "0";

            if (Context.Items.Contains("MemberNo"))
            {
                this.ViewState["MemberNo"] = Convert.ToString(Context.Items["MemberNo"]);
            }
            if (Context.Items.Contains("Sex"))
            {
                this.ViewState["Sex"] = Convert.ToString(Context.Items["Sex"]);
            }
            if (Context.Items.Contains("BuildingNo"))
            {
                this.ViewState["BuildingNo"] = Convert.ToString(Context.Items["BuildingNo"]);

            }
            if (Context.Items.Contains("FloorNo"))
            {
                this.ViewState["FloorNo"] = Convert.ToString(Context.Items["FloorNo"]);
            }
            if (Context.Items.Contains("Room"))
            {
                this.ViewState["Room"] = Convert.ToString(Context.Items["Room"]);
            }

            // 初始化控件
            initPageControls();

            ViewState["Row1"] = 0;
            ViewState["tmpMedicinId1"] = 0;
            ViewState["Row2"] = 0;
            ViewState["tmpMedicinId2"] = 0;
        }

        // get table cur rows
        if (ViewState["rowCnt"] != null)
        {
            intAddRows = (int)ViewState["rowCnt"];
        }
        // reload table
        for (int i = 0; i < intAddRows; i++)
        {
            addTableRow();
        }
    }

    // 登记按钮
    protected void btnOk_Click(object sender, EventArgs e)
    {
        int i = 0;
        int j = 0;
        string cellName = "";
        string cellApproach = "";
        string cellFrequency = "";
        string cellQuantity = "";
        string cellUnit = "";
        HealthMedicineBean beanMain = new HealthMedicineBean();
        HealthMedicineDetailBean beanDetail = new HealthMedicineDetailBean();
        HealthMedicineDao dao = new HealthMedicineDao();
        IList lstResult = new ArrayList();
        try
        {
            // do validate check
            ComValidateChecker vd = new ComValidateChecker();
            vd.CheckItems.Add(new CheckRequired(txtBeginDate.Text, new ComMessage("E0094", "开始日期")));
            vd.CheckItems.Add(new CheckDate(txtBeginDate.Text, "yyyy/MM/dd", new ComMessage("E0037", "开始日期")));
            if (lstMedicineTypeC.SelectedIndex == 1)
            {
                vd.CheckItems.Add(new CheckRequired(txtEndDate.Text, new ComMessage("E0094", "结束日期")));
                vd.CheckItems.Add(new CheckDate(txtEndDate.Text, "yyyy/MM/dd", new ComMessage("E0037", "开始日期")));
                if (!vd.DoValidator())
                {
                    ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
                    return;
                }
                else
                {
                    vd.CheckItems.Add(new CheckDateRange(DateUtils.convertToString(txtBeginDate.Text), Convert.ToString(txtEndDate.Text), new ComMessage("E0330", new string[] { "结束日期", "开始日期" })));
                    vd.CheckItems.Add(new CheckDateRange(DateUtils.convertToString(DateTime.Today), Convert.ToString(txtEndDate.Text), new ComMessage("E0330", new string[] { "结束日期", "今天" })));
                }
            }
            if (!vd.DoValidator())
            {
                ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
                return;
            }

            foreach (TableRow row in tableDyna.Rows)
            {
                if (i == 0)
                {
                    i++;
                    continue;
                }
                foreach (TableCell cell in row.Cells)
                {
                    if (j == COL_MEDICINE_NAME)
                    {
                        IEnumerator itr = cell.Controls.GetEnumerator();
                        while (itr.MoveNext())
                        {
                            TextBox txtCell = itr.Current as TextBox;
                            cellName = txtCell.Text;
                        }
                        vd.CheckItems.Add(new CheckRequired(cellName, new ComMessage("E0094", "第"+Convert.ToString(i)+"行药名")));
                        if (!vd.DoValidator())
                        {
                            ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
                            return;
                        }
                        beanDetail.MedicineName = cellName;
                    }
                    else if (j == COL_MEDICINE_APPROACH)
                    {
                        IEnumerator itr = cell.Controls.GetEnumerator();
                        while (itr.MoveNext())
                        {
                            DropDownList lstCell = itr.Current as DropDownList;
                            cellApproach = lstCell.SelectedValue;
                        }
                        beanDetail.MedicineApproach = cellApproach;
                    }
                    else if (j == COL_MEDICINE_FREQUENCY)
                    {
                        IEnumerator itr = cell.Controls.GetEnumerator();
                        while (itr.MoveNext())
                        {
                            DropDownList lstCell = itr.Current as DropDownList;
                            cellFrequency = lstCell.SelectedValue;
                        }
                        beanDetail.MedicineFrequency = cellFrequency;
                    }
                    else if (j == COL_MEDICINE_QUANTITY) 
                    {
                        IEnumerator itr = cell.Controls.GetEnumerator();
                        while (itr.MoveNext())
                        {
                            TextBox txtCell = itr.Current as TextBox;
                            cellQuantity = txtCell.Text;
                        }
                        vd.CheckItems.Add(new CheckRequired(cellQuantity, new ComMessage("E0094", "第" + Convert.ToString(i) + "行剂量")));
                        vd.CheckItems.Add(new CheckDouble(cellQuantity, 10, 2, new ComMessage("E0064", new string[] { "第" + Convert.ToString(i) + "行剂量", "10", "2" })));
                        if (!vd.DoValidator())
                        {
                            ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
                            return;
                        }
                        beanDetail.MedicineQuantity = Convert.ToDecimal(cellQuantity);
                    }
                    else if (j == COL_MEDICINE_UNIT)
                    {
                        IEnumerator itr = cell.Controls.GetEnumerator();
                        while (itr.MoveNext())
                        {
                            DropDownList lstCell = itr.Current as DropDownList;
                            cellUnit = lstCell.SelectedValue;
                        }
                        beanDetail.MedicineUnit = cellUnit;
                    }
                    j++;
                }
                lstResult.Add(beanDetail);
                beanDetail = new HealthMedicineDetailBean();
                i++;
                j = 0;
            }

            if (!vd.DoValidator())
            {
                ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
                return;
            }
            beanMain.MemberNo = Convert.ToString(this.ViewState["MEMBER_NO"]);
            beanMain.DoctorId  = Convert.ToString(this.ViewState["DOCTOR_ID"]);
            beanMain.MedicineType = Convert.ToString(lstMedicineTypeC.SelectedValue);
            beanMain.BeginDate = DateUtils.convertToDate(txtBeginDate.Text);
            if (string.IsNullOrEmpty(txtEndDate.Text))
            {
                beanMain.EndDate = null;
            }
            else
            {
                beanMain.EndDate = DateUtils.convertToDate(txtEndDate.Text);
            }
            beanMain.UserId = Convert.ToString(this.ViewState["DOCTOR_ID"]);
            beanMain.Memo = Convert.ToString(txtMemo.Text);
            dao.Insert(beanMain, lstResult);
            initDetail();
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
        }
        catch (Exception ex)
        {
            log.Fatal(ex.StackTrace);
            log.Fatal(ex.Message);
            throw ex;
        }
    }


    // 返回按钮
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "~/App/Input/HealthMedicineSrhForm.aspx";
        Context.Items["ParamPageIndex"] = ViewState["ParamPageIndex"];
        Context.Items["MemberNo"] = ViewState["MemberNo"];
        Context.Items["Sex"] = ViewState["Sex"];
        Context.Items["BuildingNo"] = ViewState["BuildingNo"];
        Context.Items["FloorNo"] = ViewState["FloorNo"];
        Context.Items["Room"] = ViewState["Room"];
        ControllerUtils.Transfer(Context, url, true);
    }

    // bind common catalog data 
    private void initPageControls()
    {
        HealthMedicineBean bean = new HealthMedicineBean();
        HealthMedicineDao dao   = new HealthMedicineDao();

        bean.MemberNo = Convert.ToString(this.ViewState["MEMBER_NO"]);
        bean.EmployeeLevel = Convert.ToString(this.ViewState["EMPLY_LEVEL"]);
        bean.DoctorId = Convert.ToString(this.ViewState["DOCTOR_ID"]);
        IList lstMember = dao.ListMember(bean);
        if (lstMember.Count == 0)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0019").ToString();
            return;
        }
        btnNew.Enabled = true;
        bean = (HealthMedicineBean)lstMember[0];
        lblDocId.Text = bean.DoctorId;
        lblMemId.Text = bean.MemberNo;
        lblMemName.Text = bean.MemberName;
        lblMemSex.Text = bean.MemberSex;
        lblMemTel.Text = bean.MemberTel;
        lblRoomId.Text = bean.ApartmentNo;
        txtBeginDate.BackColor = System.Drawing.Color.White;
        txtEndDate.BackColor = System.Drawing.Color.Silver;
        txtMemo.BackColor = System.Drawing.Color.White;

        CommonDictionaryBean beanComDict = new CommonDictionaryBean();

        // 用药类型
        log.Fatal("\n ***** get T_MEMBER_MEDICINE.MEDICINE_TYPE catalog *****");
        beanComDict.Catalog = "T_MEMBER_MEDICINE.MEDICINE_TYPE";
        this.lstMedicineType = dao.FindByCatalog(beanComDict);
        bindDataToListControl("lstMedicineTypeC",lstMedicineType);

    }

    // before render
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        HealthMedicineBean bean = new HealthMedicineBean();
        HealthMedicineDao dao = new HealthMedicineDao();
        bean.MemberNo = Convert.ToString(this.ViewState["MEMBER_NO"]);
        bean.EmployeeLevel = Convert.ToString(this.ViewState["EMPLY_LEVEL"]);
        bean.DoctorId = Convert.ToString(this.ViewState["DOCTOR_ID"]);
        IList lstMain = dao.ListMain(bean);
        if (lstMain.Count == 0)
        {
            tblMain.Visible = false;
            if(Convert.ToString(this.ViewState["NEW_FLG"]) == "0")
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0022").ToString();
            }
        }
        else
        {
            viewSrhRst.DataSource = lstMain;
            viewSrhRst.DataBind();
            tblMain.Visible = true;
        }
        bean.MemberNo = Convert.ToString(this.ViewState["MEMBER_NO"]);
        bean.EmployeeLevel = Convert.ToString(this.ViewState["EMPLY_LEVEL"]);
        bean.DoctorId = Convert.ToString(this.ViewState["DOCTOR_ID"]);
        bean.CancelFlg = true;
        IList lstFin = dao.ListMain(bean);
        if (lstFin.Count == 0)
        {
        }
        else
        {
            viewSrhFin.DataSource = lstFin;
            viewSrhFin.DataBind();
        }
        /*if (Convert.ToString(ViewState["MEMBER_MEDICINE_ID"]) != "0")
        {
            HealthMedicineDetailBean beanDetail = new HealthMedicineDetailBean();
            beanDetail.MemberMedicineId = Convert.ToInt32(ViewState["MEMBER_MEDICINE_ID"]);
            IList lstDetail = dao.ListDetail(beanDetail);
            if (lstDetail.Count != 0)
            {
                viewSrhDetail.DataSource = lstDetail;
                viewSrhDetail.DataBind();
                tblDetail.Visible = true;
            }
            else
            {
                tblDetail.Visible = false;
            }
        }
        else
        {
            tblDetail.Visible = false;
        }*/
    }

    // 新建用药信息
    protected void btnNew_Click(object sender, EventArgs e)
    {
        detail.Visible = true;
        btnOk.Enabled = true;
        btnCancel.Enabled = true;
        btnFinish.Enabled = false;
        this.ViewState["NEW_FLG"] = "1";
        ViewState["MEMBER_MEDICINE_ID"] = "0";
    }

    //取消用药信息
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        initDetail();
    }

    //用药明细初始化
    protected void initDetail()
    {
        detail.Visible = false;
        lstMedicineTypeC.SelectedIndex = 0;
        txtMemo.Text = "";
        txtBeginDate.Text = "";
        txtEndDate.Text = "";
        ViewState["rowCnt"] = DEFAULTROW_WORKEXP;
        this.tableDyna.Rows.Clear();
        this.ViewState["NEW_FLG"] = "0";
        btnOk.Enabled = false;
        btnCancel.Enabled = false;
        ViewState["MEMBER_MEDICINE_ID"] = "0";

    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }

    // page index change
    protected void viewSrhDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhDetail.PageIndex = e.NewPageIndex;
    }

    // page index change
    protected void viewSrhFin_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhFin.PageIndex = e.NewPageIndex;
    }

    // details click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Request["x"] == null || Request["y"] == null)
            return;

        if (e.CommandName == "Details")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            // Get the Key value from the GridView control.
            DataKey key = viewSrhRst.DataKeys[index];
            ViewState["MEMBER_MEDICINE_ID"] = Convert.ToString(key.Value);
            try
            {
                HealthMedicineBean bean = new HealthMedicineBean();
                HealthMedicineDao dao = new HealthMedicineDao();
                bean.MemberMedicineId = Convert.ToInt32(ViewState["MEMBER_MEDICINE_ID"]);
                dao.Finish(bean);
                btnFinish.Enabled = false;
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0023").ToString().Replace("{0}", Convert.ToString(ViewState["MEMBER_MEDICINE_ID"]));
            }
            catch (Exception ex)
            {
                log.Fatal(ex.StackTrace);
                log.Fatal(ex.Message);
                throw ex;
            }
        }
    }

    // details click
    protected void viewSrhFin_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Request["x"] == null || Request["y"] == null)
            return;

        if (e.CommandName == "Details")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            // Get the Key value from the GridView control.
            DataKey key = viewSrhFin.DataKeys[index];
            ViewState["MEMBER_MEDICINE_ID"] = Convert.ToString(key.Value);
            HealthMedicineBean bean = new HealthMedicineBean();
            HealthMedicineDao dao = new HealthMedicineDao();
            bean.MemberMedicineId = Convert.ToInt32(key.Value);
            IList lstFinish = dao.ChkFinish(bean);
            if (lstFinish.Count != 0)
            {
                btnFinish.Enabled = true;
            }
            else
            {
                btnFinish.Enabled = false;
            }

        }
    }

    // adjust bind data
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //int medicinId = 0;
        //ViewState["Row1"] = 1;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //if ((Convert.ToInt32(ViewState["MEMBER_MEDICINE_ID"]) == ((HealthMedicineBean)e.Row.DataItem).MemberMedicineId) || Convert.ToInt32(ViewState["MEMBER_MEDICINE_ID"]) == 0)
            //{
            //    foreach(TableCell cell in e.Row.Cells)
            //    {
            //        cell.BackColor = System.Drawing.Color.AntiqueWhite;
            //    }
            //}
            //else
            //{
            //    foreach (TableCell cell in e.Row.Cells)
            //    {
            //        cell.BackColor = System.Drawing.Color.LemonChiffon;
            //    }
            //}
            if (Convert.ToInt32(((HealthMedicineBean)e.Row.DataItem).MemberMedicineId) != Convert.ToInt32(ViewState["tmpMedicinId1"]))
            {
                ViewState["Row1"] = Convert.ToInt32(ViewState["Row1"]) + 1;
            }
            ViewState["tmpMedicinId1"] = Convert.ToInt32(((HealthMedicineBean)e.Row.DataItem).MemberMedicineId);
            int groupIdx = Convert.ToInt32(ViewState["Row1"]);
            if (groupIdx % 2 == 0)
            {
                foreach (TableCell cell in e.Row.Cells)
                {
                    cell.BackColor = System.Drawing.Color.LightCyan;
                }
            }
            else 
            {
                foreach (TableCell cell in e.Row.Cells)
                {
                    cell.BackColor = System.Drawing.Color.LemonChiffon;
                }
            }
            if (Convert.ToBoolean(((HealthMedicineBean)e.Row.DataItem).CancelFlg) == true || Convert.ToDateTime(((HealthMedicineBean)e.Row.DataItem).EndDate) < DateTime.Today && !string.IsNullOrEmpty(Convert.ToString(((HealthMedicineBean)e.Row.DataItem).EndDate)))
            {
                e.Row.Cells[COL_MEDICINE_TYPE].Text = "停药";
            }
            else
            {
                e.Row.Cells[COL_MEDICINE_TYPE].Text = "正常";
            }
            ViewState["MEMBER_MEDICINE_ID"] = Convert.ToString(((HealthMedicineBean)e.Row.DataItem).MemberMedicineId);

            //HyperLink Hlink = ((HyperLink)e.Row.FindControl("HyperLink1"));
            //Hlink.Attributes.Add("onclick", "selectPhysical(" + e.Row.Cells[1].Text + ")");
        }
    }
    // adjust bind data
    protected void viewSrhFin_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (Convert.ToInt32(((HealthMedicineBean)e.Row.DataItem).MemberMedicineId) != Convert.ToInt32(ViewState["tmpMedicinId2"]))
            {
                ViewState["Row2"] = Convert.ToInt32(ViewState["Row2"]) + 1;
            }
            ViewState["tmpMedicinId2"] = Convert.ToInt32(((HealthMedicineBean)e.Row.DataItem).MemberMedicineId);
            int groupIdx = Convert.ToInt32(ViewState["Row2"]);
            if (groupIdx % 2 == 0)
            {
                foreach (TableCell cell in e.Row.Cells)
                {
                    cell.BackColor = System.Drawing.Color.LightCyan;
                }
            }
            else
            {
                foreach (TableCell cell in e.Row.Cells)
                {
                    cell.BackColor = System.Drawing.Color.LemonChiffon;
                }
            }
            if (Convert.ToBoolean(((HealthMedicineBean)e.Row.DataItem).CancelFlg) == true || Convert.ToDateTime(((HealthMedicineBean)e.Row.DataItem).EndDate) < DateTime.Today && !string.IsNullOrEmpty(Convert.ToString(((HealthMedicineBean)e.Row.DataItem).EndDate)))
            {
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[4].Text = "停药";
            }
            else
            {
                e.Row.Cells[4].Text = "正常";
            }
            ViewState["MEMBER_MEDICINE_ID"] = Convert.ToString(((HealthMedicineBean)e.Row.DataItem).MemberMedicineId);
        }
    }
    // row add
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        addTableRow();
        ViewState["rowCnt"] = ++intAddRows;
    }

    // row del
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (tableDyna.Rows.Count > DEFAULTROW_WORKEXP + 1)
        {
            delTableRow(-1);
            ViewState["rowCnt"] = --intAddRows;
        }
    }

    // add one row to table
    private void addTableRow()
    {
        HealthMedicineDao dao = new HealthMedicineDao();
        CommonDictionaryBean beanComDict = new CommonDictionaryBean();
        // 用药途径
        log.Fatal("\n ***** get MEDICINE_APPROACH catalog *****");
        beanComDict.Catalog = "MEDICINE_APPROACH";
        this.lstMedicineApproach = dao.FindByCatalog(beanComDict);

        // 用药频次
        log.Fatal("\n ***** get MEDICINE_FREQUENCY catalog *****");
        beanComDict.Catalog = "MEDICINE_FREQUENCY";
        this.lstMedicineFrequency = dao.FindByCatalog(beanComDict);

        // 药品单位
        log.Fatal("\n ***** get MEDICINE_UNIT catalog *****");
        beanComDict.Catalog = "MEDICINE_UNIT";
        this.lstMedicineUnit = dao.FindByCatalog(beanComDict);

        TableRow row = new TableRow();
        int intRowNo = this.tableDyna.Rows.Count;
        for (int i = 0; i < DETAIL_COL_COUNT; i++)
        {
            TableCell cell = new TableCell();
            if (i == COL_MEDICINE_APPROACH)
            {
                DropDownList addLst = new DropDownList();
                addLst.ID = "lstCell" + +(intRowNo - 1) + "_" + i;
                foreach (CommonDictionaryBean bean in lstMedicineApproach)
                {
                    addLst.Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
                }
                cell.Controls.Add(addLst);
                row.Cells.Add(cell);
                continue;
            }
            else if (i == COL_MEDICINE_FREQUENCY)
            {
                DropDownList addLst = new DropDownList();
                addLst.ID = "lstCell" + +(intRowNo - 1) + "_" + i;
                foreach (CommonDictionaryBean bean in lstMedicineFrequency)
                {
                    addLst.Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
                }
                cell.Controls.Add(addLst);
                row.Cells.Add(cell);
                continue;
            }
            else if (i == COL_MEDICINE_UNIT)
            {
                DropDownList addLst = new DropDownList();
                addLst.ID = "lstCell" + +(intRowNo - 1) + "_" + i;
                foreach (CommonDictionaryBean bean in lstMedicineUnit)
                {
                    addLst.Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
                }
                cell.Controls.Add(addLst);
                row.Cells.Add(cell);
                continue;
            }
            TextBox addTxtBox = new TextBox();
            addTxtBox.ID = "txtCell" + (intRowNo - 1) + "_" + i;
            addTxtBox.Text = "";
            cell.Controls.Add(addTxtBox);
            row.Cells.Add(cell);
        }
        this.tableDyna.Rows.Add(row);
    }


    // del one row to table
    private void delTableRow(int rowIndex)
    {
        if (rowIndex == -1) rowIndex = this.tableDyna.Rows.Count - 1;
        if (rowIndex > 0) this.tableDyna.Rows.RemoveAt(rowIndex);
    }

    // bind db data to selected controls
    private void bindDataToListControl(string ControlId, IList LstData)
    {
        if (LstData == null || LstData.Count == 0) return;

        foreach (CommonDictionaryBean bean in LstData)
        {
            if (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is DropDownList)
            {
                ((DropDownList)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
            else if ((this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is CheckBoxList)
                || (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is RadioButtonList))
            {
                ((ListControl)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
        }
    }


    protected void lstMedicineTypeC_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.lstMedicineTypeC.SelectedIndex == 0)
        {
            txtEndDate.Text = "";
            txtEndDate.Enabled = false;
            txtEndDate.BackColor = System.Drawing.Color.Silver;
        }
        else
        {
            txtEndDate.Enabled = true;
            txtEndDate.BackColor = System.Drawing.Color.White;
        }
    }
    protected void btnFinish_Click(object sender, EventArgs e)
    {
    }
    protected void btnHistory_Click(object sender, EventArgs e)
    {
        if (Convert.ToString(ViewState["HISTORY_FLG"]) == "0")
        {
            HealthMedicineBean bean = new HealthMedicineBean();
            HealthMedicineDao dao = new HealthMedicineDao();
            bean.MemberNo = Convert.ToString(this.ViewState["MEMBER_NO"]);
            bean.EmployeeLevel = Convert.ToString(this.ViewState["EMPLY_LEVEL"]);
            bean.DoctorId = Convert.ToString(this.ViewState["DOCTOR_ID"]);
            bean.CancelFlg = true;
            IList lstFin = dao.ListMain(bean);
            if (lstFin.Count == 0)
            {
                tblHistory.Visible = false;
                btnHistory.Enabled = false;
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0024").ToString();
            }
            else
            {
                viewSrhFin.DataSource = lstFin;
                viewSrhFin.DataBind();
                tblHistory.Visible = true;
                ViewState["HISTORY_FLG"] = "1";
                ViewState["MEMBER_MEDICINE_ID"] = "0";
                btnHistory.Text = "隐藏历史记录";
            }

        }
        else
        {
            tblHistory.Visible = false;
            ViewState["HISTORY_FLG"] = "0";
            ViewState["MEMBER_MEDICINE_ID"] = "0";
            btnHistory.Text = "显示历史记录";
        }
    }
}
